package com.itheima.ydd.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.ydd.common.PageBean;
import com.itheima.ydd.common.R;
import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.dto.YddAdvimgDto;
import com.itheima.ydd.dto.YddAdvposDto;
import com.itheima.ydd.entity.YddAdvimg;
import com.itheima.ydd.entity.YddAdvpos;
import com.itheima.ydd.service.IYddAdvimgService;
import com.itheima.ydd.service.IYddAdvposService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2022-05-22
 */
@RestController
@RequestMapping("/ydd_advpos")

public class YddAdvposController {
    @Autowired
    private IYddAdvposService iYddAdvposService;
    @Autowired
    private IYddAdvimgService iYddAdvimgService;

    @GetMapping("/list")
    public ResultData page(String advposname,int page,Long limit){
        Page<YddAdvpos> pageInfo=new Page<>(page,limit);
        LambdaQueryWrapper<YddAdvpos>lqw=new LambdaQueryWrapper<>();
        lqw.like(advposname!=null,YddAdvpos::getAdvposname,advposname);
        iYddAdvposService.page(pageInfo,lqw);

        List<YddAdvposDto>yddAdvposDtos =pageInfo.getRecords().stream().map(
                item->{
                    YddAdvposDto yddAdvposDto=new YddAdvposDto();
                    BeanUtils.copyProperties(item,yddAdvposDto);

                    LambdaQueryWrapper<YddAdvimg> lambdaQueryWrapper=new LambdaQueryWrapper<>();
                   //获取广告位ID,查广告图的pos
                    lambdaQueryWrapper.eq(YddAdvimg::getAdvimgpos,item.getId());
                    //imgservice调用聚合函数查
                    int count = iYddAdvimgService.count(lambdaQueryWrapper);
                    yddAdvposDto.setAdvposnum(count);
                    return yddAdvposDto;

                }
        ).collect(Collectors.toList());



        PageBean<YddAdvposDto> pageBean=new PageBean<>();
        pageBean.setData(yddAdvposDtos);//封装的具体集合数据
        pageBean.setPageSize(pageInfo.getSize());//每页条数
        pageBean.setCount(pageInfo.getTotal());//总条数
        pageBean.setTotalPages(pageInfo.getPages());//总页数
        pageBean.setCurrentPage(pageInfo.getCurrent());//当前页
        return ResultData.ok("",pageBean);
    }

    @PostMapping("/add")
    public ResultData save(@RequestBody YddAdvpos yddAdvpos){
        iYddAdvposService.save(yddAdvpos);
        return ResultData.ok("添加成功",yddAdvpos);
    }

    @GetMapping("/rowInfo")
    public ResultData getById(@RequestParam Integer id){
        LambdaQueryWrapper<YddAdvpos>lqw=new LambdaQueryWrapper<>();
        lqw.eq(YddAdvpos::getId,id);

        YddAdvpos one = iYddAdvposService.getOne(lqw);

        return ResultData.ok("",one);
    }

    @PostMapping("/update")
    public ResultData update(@RequestBody YddAdvpos yddAdvpos){
        LambdaUpdateWrapper<YddAdvpos> luw=new LambdaUpdateWrapper<>();
        luw.eq(YddAdvpos::getId,yddAdvpos.getId());
        iYddAdvposService.update(yddAdvpos,luw);
        return ResultData.ok("修改成功",yddAdvpos);
    }

    //需要修改
    @GetMapping("/del")
    public ResultData delById(@RequestParam Integer id){
        LambdaQueryWrapper<YddAdvpos>lqw=new LambdaQueryWrapper<>();
        lqw.eq(YddAdvpos::getId,id);
        iYddAdvposService.remove(lqw);
        return ResultData.ok("","删除成功");
    }

    @GetMapping("/all")
    public ResultData getAll(){
        List<YddAdvpos> list = iYddAdvposService.list();
        if(list.size()>0){
            return ResultData.ok("",list);
        }
        return ResultData.error("暂无数据");

    }

}
